/*
 * @Author: your name
 * @Date: 2021-01-28 11:22:53
 * @LastEditTime: 2021-04-18 12:17:51
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense\assets\scripts\game_scene\tower\warlock_tower.ts
 */

import { WarlockTowerParams } from "../../game_data/tower_data/warlock_tower_params";
import { UG } from "../../modules/ugame";
import { FrameAnim } from "../../common/frame_anim";
import { WarlockBullet } from "../bullet/warlock_bullet";

const { ccclass, property } = cc._decorator;

// 自定义类
@ccclass("warlock_tower_skin")
class warlock_tower_skin {
    @property(cc.SpriteFrame)   // 塔资源
    public tower_anim: Array<cc.SpriteFrame> = [];

    @property(cc.Integer)       // 动画帧速率
    public tower_anim_sample: number = 10;

    @property(cc.Integer)       // 人物x
    public xpos: number = -1;

    @property(cc.Integer)       // 人物y
    public ypos: number = 19;
}

@ccclass
export class WarlockTower extends cc.Component {

    @property(cc.Integer)   // 塔等级
    public tower_level: number = 1;

    @property(cc.Node)      // 子弹精灵节点
    public anim_node: cc.Node = null;

    @property(cc.Node)      // 人物节点
    public man_node: cc.Node = null;

    @property(cc.Graphics)  // 画笔
    public graphics: cc.Graphics = null;

    @property(cc.Prefab)    // 子弹预制体
    public warlock_bullet_pre: cc.Prefab = null;

    @property({             // 塔每个等级的皮肤
        type: warlock_tower_skin,
    })
    public level_tower_skin = [];

    @property(cc.Integer)       // 动画帧速率
    public shoot_anim_sample: number = 10;

    @property(cc.SpriteFrame)   // 向上发射的人动画
    public shoot_up_anim: Array<cc.SpriteFrame> = [];

    @property(cc.SpriteFrame)   // 向下发射的人动画
    public shoot_down_anim: Array<cc.SpriteFrame> = [];

    public warlock_tower_frame_anim: FrameAnim = null;    // 保存自定义动画组件
    public man_frame_anim: FrameAnim = null;              // 人物动画组件

    public bullet_root_node: cc.Node = null;    // 子弹存放节点
    public is_debug: boolean = true;           // 是否开启塔攻击范围绘制
    public tower_center_pos: cc.Vec2 = cc.v2();     // 塔中心坐标
    public tower_center_pos_in_tower_panel: cc.Vec2 = null; // tower_panel坐标系下的塔中心节点

    start() {
        this.scheduleOnce(this.warlock_tower_ai.bind(this), 0.1);
    }

    onLoad() {
        // this.is_debug = true;

        this.bullet_root_node = cc.find('Canvas/tower_panel/bullet_root');

        // 获取塔中心坐标
        this.tower_center_pos = this.get_tower_center_pos();

        // 开启debug
        // this._draw_tower_debug();

        // 绑定自定义动画组件
        this.warlock_tower_frame_anim = this.anim_node.addComponent(FrameAnim);
        this.warlock_tower_frame_anim.sprite_com = this.anim_node.getComponent(cc.Sprite);
        this.warlock_tower_frame_anim.anim_com = this.anim_node.getComponent(cc.Animation);

        // 人物绑定自定义动画组件
        this.man_frame_anim = this.man_node.addComponent(FrameAnim);
        this.man_frame_anim.sprite_com = this.man_node.getComponent(cc.Sprite);
        this.man_frame_anim.anim_com = this.man_node.getComponent(cc.Animation);
    }

    // 获取塔等级
    public get_tower_level(): any {
        if (this.tower_level >= 4) {
            // 防御塔已经是最高级
            return true;
        } else {
            return this.tower_level;
        }
    }

    // 调整人物图片
    private _set_man_sprite(dir: number): void {
        // 0 向上, 1 向下
        let man_sprite: cc.Sprite = this.man_node.getComponent(cc.Sprite);
        if (dir === 0) {
            man_sprite.spriteFrame = this.shoot_up_anim[0];
        } else {
            man_sprite.spriteFrame = this.shoot_down_anim[0];
        }
    }

    // 根据等级设置塔图片
    private _set_tower_skin_by_level(): void {
        // 调整人物位置
        this.man_node.x = this.level_tower_skin[this.tower_level - 1].xpos;
        this.man_node.y = this.level_tower_skin[this.tower_level - 1].ypos;

        // 调整塔精灵图片
        let sprite: cc.Sprite = this.anim_node.getComponent(cc.Sprite);
        sprite.spriteFrame = this.level_tower_skin[this.tower_level - 1].tower_anim[0];

        // 调整人物精灵图片
        this._set_man_sprite(1);
    }

    // 升级塔
    public upgrade_tower(): any {
        // 弓箭塔有4个等级
        if (this.tower_level >= 4) {
            cc.log("已经最高级");
            return true;
        }

        // 开启debug
        this._draw_tower_debug();

        this.tower_level++;
        // 根据等级设置弓箭塔精灵
        this._set_tower_skin_by_level();

        return this.tower_level;
    }

    // 获取塔中心位置
    public get_tower_center_pos(): cc.Vec2 {
        let center_pos: cc.Vec2 = this.node.getPosition();

        let _a: cc.Vec2 = this.node.parent.convertToWorldSpaceAR(this.tower_center_pos);   // 转为世界坐标
        let _b: cc.Vec2 = this.node.parent.parent.convertToNodeSpaceAR(_a);

        this.tower_center_pos_in_tower_panel = _b;

        // this.graphics.circle(_b.x, _b.y, 5);
        // this.graphics.fill();

        return center_pos;
    }

    // 绘制塔攻击范围
    private _draw_tower_debug(): void {
        if (!this.is_debug) {
            return;
        }
        // 先清除s
        this.graphics.clear();
        // 搜索半径
        let search_r: number = WarlockTowerParams[this.tower_level - 1].search_R;
        // 绘制
        const _b: cc.Vec2 = this.tower_center_pos_in_tower_panel;
        this.graphics.circle(_b.x, _b.y, search_r);
        this.graphics.stroke();
    }

    // ===================================== 塔行为 ========================================

    // 人物动画
    private _man_anim(enemy_world_pos: cc.Vec2): void {
        // 计算方向
        let man_world_pos: cc.Vec2 = this.man_node.parent.convertToWorldSpaceAR(cc.v2(this.man_node.x, this.man_node.y));
        let dir: cc.Vec2 = enemy_world_pos.sub(man_world_pos);

        let _dir_num: number = 0;
        if (dir.y > 0) {    // 向上
            this.man_frame_anim.sprite_frame_arr = this.shoot_up_anim;
            _dir_num = 0;
        } else {    // 向下
            this.man_frame_anim.sprite_frame_arr = this.shoot_down_anim;
            _dir_num = 1;
        }
        this.man_frame_anim.sample = this.shoot_anim_sample;

        // 播放一次动画
        this.man_frame_anim.play_once(() => {
            // 播放结束，恢复人物图片 默认向下
            this._set_man_sprite(_dir_num);
        })
    }

    // 发射动画
    private _shoot_warlock_bullet_anim(level: number, enemy_world_pos: cc.Vec2, enemy: cc.Node): void {
        let man_world_pos: cc.Vec2 = this.man_node.parent.convertToWorldSpaceAR(cc.v2(this.man_node.x, this.man_node.y));
        let dir: cc.Vec2 = enemy_world_pos.sub(man_world_pos);

        // 朝上
        const up_start_pos_arr: Array<cc.Vec2> = [cc.v2(5, 25), cc.v2(7, 25), cc.v2(6, 29), cc.v2(8, 24)];
        // 朝下
        const down_start_pos_arr: Array<cc.Vec2> = [cc.v2(-6, 25), cc.v2(-4, 25), cc.v2(-6, 29), cc.v2(-2, 24)];
        
        let start_world_pos: cc.Vec2 = cc.v2();
        if (dir.y > 0) {    // 向上
            start_world_pos = this.anim_node.convertToWorldSpaceAR(up_start_pos_arr[this.tower_level - 1]);
            this._set_man_sprite(0);
        } else {
            start_world_pos = this.anim_node.convertToWorldSpaceAR(down_start_pos_arr[this.tower_level - 1]);
            this._set_man_sprite(1);
        }

        // 发射子弹
        let warlock_bullet_node: cc.Node = cc.instantiate(this.warlock_bullet_pre);
        warlock_bullet_node.parent = this.bullet_root_node;
        let warlock_bullet_com: WarlockBullet = warlock_bullet_node.getComponent(WarlockBullet);
        warlock_bullet_com.bullet_level = level;
        warlock_bullet_com.shoot(level, start_world_pos, enemy_world_pos, enemy);
    }

    // 射击
    public shoot_anim(enemy_world_pos: cc.Vec2, enemy: cc.Node): void {
        // 塔
        this.warlock_tower_frame_anim.sprite_frame_arr = this.level_tower_skin[this.tower_level - 1].tower_anim;
        this.warlock_tower_frame_anim.sample = this.level_tower_skin[this.tower_level - 1].tower_anim_sample;

        // 播放一次塔蓄力动画
        this.warlock_tower_frame_anim.play_once(() => {
            // 播放结束，恢复塔图片
            this._set_tower_skin_by_level();
            // 播放人物动画
            this._man_anim(enemy_world_pos);
        })

        // 人物动画
        // this._man_anim(enemy_world_pos);
        // 发射动画
        this._shoot_warlock_bullet_anim(this.tower_level, enemy_world_pos, enemy);
    }

    // 塔AI
    public warlock_tower_ai(): void {
        if (!this.tower_center_pos) {
            this.tower_center_pos = this.get_tower_center_pos();
        }
        // 获取塔中心位置
        let center_pos: cc.Vec2 = this.tower_center_pos;
        // 搜索半径
        let search_r = WarlockTowerParams[this.tower_level - 1].search_R;
        // 扫描时间间隔
        let time: number = 0.1;
        // 攻击敌人
        let enemy: cc.Node = UG.search_enemy(center_pos, search_r);
        if (enemy) {
            let _enemy_world_pos: cc.Vec2 = enemy.parent.convertToWorldSpaceAR(cc.v2(enemy.x, enemy.y));   // 再敌人的父节点中，把敌人转为世界坐标
            // 播放射击动画
            this.shoot_anim(_enemy_world_pos, enemy);
            time = 2;
        }
        this.scheduleOnce(this.warlock_tower_ai.bind(this), time);
    }

}
